#include <cstdio>
#include <vector>
#include <algorithm>

#define INF 0x3f3f3f3f3f3f3f3fll

int a[200005];

long long solve(int n, int m){
    std::vector<std::vector<long long>> 
        res0(n+5, std::vector<long long>(m+5, -INF)),
        res1(n+5, std::vector<long long>(m+5, -INF));
    
    std::vector<long long> 
        max0(n+5, -INF),
        max1(n+5, -INF);

    res0[0][0] = res1[0][0] = 0;
    max0[0] = max1[0] = 0;

    for (int i=1; i<=n; ++i){
        for (int j=0; j<=m; ++j){
            res0[i][j] = std::max(res0[i][j], res0[i-1][j]+(a[i]==a[i-1]?a[i]:0));
            if (a[i-1] == j)
                res0[i][j] = std::max({res0[i][j], res1[i-1][a[i]]+a[i], max1[i-1]});
            res1[i][j] = std::max(res1[i][j], res1[i-1][j]+(a[i]==a[i-1]?a[i]:0));
            if (a[i-1] == j)
                res1[i][j] = std::max({res1[i][j], res0[i-1][a[i]]+a[i], max0[i-1]});
            max0[i] = std::max(max0[i], res0[i][j]);
            max1[i] = std::max(max1[i], res1[i][j]);
        }
    }

    return std::max(max0[n], max1[n]);
}

int main(){
    freopen("color.in" , "r", stdin );
    freopen("color.out", "w", stdout);

    int t;
    scanf("%d", &t);

    while (t--){
        int n, m = 0;
        scanf("%d", &n);

        for (int i=1; i<=n; ++i){
            scanf("%d", a+i);
            m = std::max(m, a[i]);
        }
        
        printf("%lld\n", solve(n, m));

    }

    return 0;
}

